/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aaron.platform.view;

import com.aaron.platform.dao.PageInfo;
import com.aaron.platform.entity.FgysEnterprise;
import com.aaron.platform.exceptions.EnterpriseActiveException;
import com.aaron.platform.exceptions.EnterpriseAllreadyExistException;
import com.aaron.platform.exceptions.EnterpriseNotExistException;
import com.aaron.platform.service.IEnterpriseMgr;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;


/**
 *
 * @author aaronbamboo
 */
public class EnterpriseListBean extends DataListBean {
    
    private static Logger logger = Logger.getLogger(EnterpriseListBean.class);
    
    private IEnterpriseMgr enterpriseMgr;
    
    private String enterpriseName;
    
    private FgysEnterprise currentEnterprise;
    
    /** 1:view 2:edit 3:delete */
    private int currentOperation = 1;

    public EnterpriseListBean() {
        super();
        init();
        this.tableTitle = FacesUtil.getString("table_title_Enterpriselist");

        // 删除、编辑、查看操作
        this.deletable = true;
        this.editable = true;
        this.viewable = true;
        
        // 设置分页信息
        pagable = true;
    }

    private void init() {
        this.enterpriseName = "";
        pageInfo = new PageInfo<FgysEnterprise>();
    }
    
    
    
    @Override
    public void queryData() {
        Properties properties = null;
        if (!StringUtils.isEmpty(enterpriseName)) {
            properties = new Properties();
            properties.setProperty(FgysEnterprise.ENTERPRISE_NAME, StringUtils.trim(enterpriseName));
        } 
        
        pageInfo = enterpriseMgr.findEnterprisesByCondition(properties, pageInfo.getStartIndex(), pageInfo.getPageSize());
        logger.info("Query enterprise information list finished.");
        data.setWrappedData(pageInfo.getItems());
    }

    @Override
    public void initColumnHeaders() {
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "enterprise_name", 
                FacesUtil.getString("table_column_enterprise_name"), false, "25%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "enterprise_contactor",
                FacesUtil.getString("table_column_enterprise_contactor"), false, "10%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "contactor_phone",
                FacesUtil.getString("table_column_contactor_phone"), false, "15%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "contactor_email",
                FacesUtil.getString("table_column_contactor_email"), false, "20%"));
    }

    @Override
    public String getColumnValue() {
        String columnName = getColumnName();
        increaseColumnIndex();
        
        FgysEnterprise obj = (FgysEnterprise) data.getRowData();
        if (columnName.equals("enterprise_name")) {
            return obj.getEnterpriseName();
        } else if (columnName.equals("enterprise_contactor")) {
            return obj.getEnterpriseContactor();
        } else if (columnName.equals("contactor_phone")) {
            return obj.getEnterprisePhone();
        } else if (columnName.equals("contactor_email")) {
            return obj.getEnterpriseEmail();
        } else {
            return null;
        }
    }
    
     /**
     * 导航到供应商添加页面
     * @return 供应商添加页面
     */
    public String toAddEnterprise() {
        currentEnterprise = new FgysEnterprise();
        currentOperation = 3;
        return "enterprise_add";
    }
    
    /**
     * 返回上一页
     * @return 上一页
     */
    public String toLastPage() {
        return "enterprise_search";
    }
    
    @Override
    public String editData() {
        currentEnterprise = (FgysEnterprise) data.getRowData();
        currentOperation = 2;
        return "enterprise_add";
    }
    
    @Override
    public String deleteData() {
        currentEnterprise = (FgysEnterprise) data.getRowData();
        try {
            enterpriseMgr.removeEnterprise(currentEnterprise.getEnterpriseId());
        } catch (EnterpriseActiveException ex) {
            FacesUtil.addErrorMessage(null, "er_enterprise_payment_exist");
            return null;
        }
        // 刷新数据
        queryData();
        return "enterprise_search";
    }

     @Override
    public String showDetail() {
        currentEnterprise = (FgysEnterprise) data.getRowData();
        currentOperation = 1;
        return "enterprise_add";
    }
     
    public String saveEnterprise() {
        
        // 判断输入条件
        if (StringUtils.isBlank(currentEnterprise.getEnterpriseName())) {
            FacesUtil.addErrorMessage(null, "er_enterprise_name_required");
            return null;
        } 
        
        if (currentOperation == 2) {
            try {
                enterpriseMgr.updateEnterprise(currentEnterprise);
            } catch (EnterpriseAllreadyExistException ex) {
                FacesUtil.addErrorMessage(null, "er_enterprise_allready_exist");
                return null;
            } catch (EnterpriseNotExistException ex) {
                FacesUtil.addErrorMessage(null, "er_enterprise_not_exist");
                return null;
            }
            FacesUtil.addInfoMessage(null, "info_update_enterprise_success", currentEnterprise.getEnterpriseName());
        } else if (currentOperation == 3) {
            try {
                enterpriseMgr.addEnterprise(currentEnterprise);
            } catch (EnterpriseAllreadyExistException ex) {
                FacesUtil.addErrorMessage(null, "er_enterprise_allready_exist");
                return null;
            }
            FacesUtil.addInfoMessage(null, "info_add_enterprise_success", currentEnterprise.getEnterpriseName());
        }
        
        return "enterprise_search";
    }

    public FgysEnterprise getCurrentEnterprise() {
        return currentEnterprise;
    }

    public void setCurrentEnterprise(FgysEnterprise currentEnterprise) {
        this.currentEnterprise = currentEnterprise;
    }

    public String getEnterpriseName() {
        return enterpriseName;
    }

    public void setEnterpriseName(String enterpriseName) {
        this.enterpriseName = enterpriseName;
    }
     
    public void setEnterpriseMgr(IEnterpriseMgr enterpriseMgr) {
        this.enterpriseMgr = enterpriseMgr;
    }

    public int getCurrentOperation() {
        return currentOperation;
    }
}
